package com.jingtuo.android.common.widget;


import android.content.Context;
import android.graphics.Matrix;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

public class MultiTouchView extends ImageView {

	public static final String TAG = MultiTouchView.class.getName();

	protected ScaleGestureDetector mScaleGestureDetector;

	protected GestureDetectorCompat mGestureDetectorCompat;

	public static final float SCALE_MIN = 0.5f;

	public static final float SCALE_MAX = 2.0f;
	
	public static final float SCALE_INIT = 1.0F;
	
	protected float mTotalScale = SCALE_INIT;
	
	protected Matrix mSaveMatrix = new Matrix();
	
	protected Matrix mOperateMatrix = new Matrix();
	
	protected boolean dragEnabled = true;
	
	protected boolean zoomEnabled = true;
	
	public MultiTouchView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MultiTouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MultiTouchView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	protected void init(Context context) {
		mSaveMatrix = getImageMatrix();
		mOperateMatrix.set(mSaveMatrix);
		
		mScaleGestureDetector = new ScaleGestureDetector(context, mScaleGestureListener);
		mGestureDetectorCompat = new GestureDetectorCompat(context, mGestureListener);
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		boolean scaleTouchEvent = mScaleGestureDetector.onTouchEvent(event);
		boolean gestureTouchEvent = mGestureDetectorCompat.onTouchEvent(event);
		return scaleTouchEvent||gestureTouchEvent||super.onTouchEvent(event);
	}
	
	private ScaleGestureDetector.SimpleOnScaleGestureListener mScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener(){

		protected float previousScale = 1.0f;
		
		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			if(zoomEnabled){
				float scale = detector.getScaleFactor();
				float totalScale = mTotalScale * scale;
				if(previousScale>scale){//缩小
					if(totalScale>=SCALE_MIN){
						mOperateMatrix.postScale(scale, scale, getLeft() + getWidth()/2, getTop() + getHeight()/2);//此处不能用setScale
						setImageMatrix(mOperateMatrix);
						mTotalScale = totalScale;
					}
				}else if(previousScale<scale){//放大
					if(totalScale<=SCALE_MAX){
						mOperateMatrix.postScale(scale, scale, getLeft() + getWidth()/2, getTop() + getHeight()/2);//此处不能用setScale
						setImageMatrix(mOperateMatrix);
						mTotalScale = totalScale;
					}
				}
			}
			return true;
		}

		@Override
		public boolean onScaleBegin(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			if(zoomEnabled){
				previousScale = detector.getScaleFactor();
			}
			return true;
		}

		@Override
		public void onScaleEnd(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			super.onScaleEnd(detector);
			if(zoomEnabled){
				if(mTotalScale<SCALE_INIT){//缩小需要还原
					mOperateMatrix.set(mSaveMatrix);//此处不能用postConCat
					setImageMatrix(mOperateMatrix);
					mTotalScale = SCALE_INIT;
				}else if(mTotalScale>SCALE_INIT){//放大不作处理
//					mTotalScale = SCALE_INIT;
				}
			}
		}
	};
	
	
	private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener(){

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			if(dragEnabled){
				float dx = 0.0f - distanceX;
				float dy = 0.0f - distanceY;
				if(dx!=0.0f||dy!=0.0f){
					mOperateMatrix.postTranslate(dx, dy);
					setImageMatrix(mOperateMatrix);
				}
			}
			return true;
		}
	};

	public boolean isDragEnabled() {
		return dragEnabled;
	}

	public void setDragEnabled(boolean dragEnabled) {
		this.dragEnabled = dragEnabled;
	}

	public boolean isZoomEnabled() {
		return zoomEnabled;
	}

	public void setZoomEnabled(boolean zoomEnabled) {
		this.zoomEnabled = zoomEnabled;
	}
	
	
	
	
}
